package io.youngledo.usb;

import io.youngledo.usb.connection.StorageConnection;
import io.youngledo.usb.storage.StorageType;

/**
 * 树视图节点数据
 * 用于表示存储服务类型、连接、存储桶等不同层级的节点
 */
public class TreeNodeData {

    public enum NodeType {
        ROOT,           // 根节点
        STORAGE_TYPE,   // 存储类型节点（如MinIO、阿里云OSS）
        CONNECTION,     // 连接节点（如MinIO-开发环境）
        BUCKET          // 存储桶节点
    }

    private final NodeType nodeType;
    private final String displayText;
    private StorageType storageType;
    private StorageConnection connection;
    private String bucketName;

    // 根节点构造器
    public TreeNodeData(String displayText) {
        this.nodeType = NodeType.ROOT;
        this.displayText = displayText;
    }

    // 存储类型节点构造器
    public TreeNodeData(StorageType storageType) {
        this.nodeType = NodeType.STORAGE_TYPE;
        this.storageType = storageType;
        this.displayText = storageType.getDisplayName();
    }

    // 连接节点构造器
    public TreeNodeData(StorageConnection connection) {
        this.nodeType = NodeType.CONNECTION;
        this.connection = connection;
        this.storageType = connection.getConfig().type();
        this.displayText = connection.getDisplayName() +
                          (connection.isConnected() ? " ✓" : " ✗");
    }

    // 存储桶节点构造器
    public TreeNodeData(StorageConnection connection, String bucketName) {
        this.nodeType = NodeType.BUCKET;
        this.connection = connection;
        this.storageType = connection.getConfig().type();
        this.bucketName = bucketName;
        this.displayText = bucketName;
    }

    // Getters
    public NodeType getNodeType() {
        return nodeType;
    }

    public String getDisplayText() {
        return displayText;
    }

    public StorageType getStorageType() {
        return storageType;
    }

    public StorageConnection getConnection() {
        return connection;
    }

    public String getBucketName() {
        return bucketName;
    }

    // 判断方法
    public boolean isRoot() {
        return nodeType == NodeType.ROOT;
    }

    public boolean isStorageType() {
        return nodeType == NodeType.STORAGE_TYPE;
    }

    public boolean isConnection() {
        return nodeType == NodeType.CONNECTION;
    }

    public boolean isBucket() {
        return nodeType == NodeType.BUCKET;
    }

    @Override
    public String toString() {
        return displayText;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;

        TreeNodeData that = (TreeNodeData) obj;

        if (nodeType != that.nodeType) return false;
        if (!displayText.equals(that.displayText)) return false;

        return switch (nodeType) {
            case STORAGE_TYPE -> storageType == that.storageType;
            case CONNECTION -> connection != null && connection.equals(that.connection);
            case BUCKET -> connection != null && connection.equals(that.connection) &&
                    bucketName != null && bucketName.equals(that.bucketName);
            default -> true;
        };
    }

    @Override
    public int hashCode() {
        int result = nodeType.hashCode();
        result = 31 * result + displayText.hashCode();

        result = switch (nodeType) {
            case STORAGE_TYPE -> 31 * result + (storageType != null ? storageType.hashCode() : 0);
            case CONNECTION -> 31 * result + (connection != null ? connection.hashCode() : 0);
            case BUCKET -> {
                result = 31 * result + (connection != null ? connection.hashCode() : 0);
                yield 31 * result + (bucketName != null ? bucketName.hashCode() : 0);
            }
            default -> result;
        };

        return result;
    }
}